﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class DeleteMaterial : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                //批量删除
                if (Request["ajax"] == "dels")
                {
                    String ids = Request["ids"];
                    if (!String.IsNullOrEmpty(ids))
                    {
                        String[] idArray = ids.Split(',');
                        foreach (String id in idArray)
                        {
                            try
                            {
                                DeleteData(Int32.Parse(id), session);
                            }
                            catch (System.Exception ex)
                            {
                                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作失败-" + ex.Message });
                                return;
                            }
                        }
                        session.Flush();
                    }
                }
                //单个删除
                else if (!String.IsNullOrEmpty(Request["uid"]))
                {
                    try
                    {
                        DeleteData(Int32.Parse(Request["uid"]), session);
                    }
                    catch (System.Exception ex)
                    {
                        _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作失败-" + ex.Message });
                        return;
                    }
                }
                else
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作失败-参数错误!" });
                }
            }

            _framePage.Alert(new ReturnCode() { statusCode = 200, message = "操作成功!", navTabId = _framePage.NavTabId });
        }

        protected void DeleteData(int id, ISession session)
        {
            session.CreateQuery("delete WxMaterial where Id = :id and OwnerAccount = :account").SetParameter("account", _userAccount).SetParameter("id", id).ExecuteUpdate();
            session.CreateQuery("delete WxMaterialText where Id = :id and OwnerAccount = :account").SetParameter("account", _userAccount).SetParameter("id", id).ExecuteUpdate();
            session.CreateQuery("delete WxMaterialNews where MaterialId = :id and OwnerAccount = :account").SetParameter("account", _userAccount).SetParameter("id", id).ExecuteUpdate();
            session.Flush();
        }
    }
}